var url = decodeURI(window.location.href);

var argsIndex = url .split("?bvid=");

var bv = argsIndex[1]

var lv = 1;
var sex = true;
var fensan = true;

function cal(){
	  var calbtn;
	  
	  if (lv < 5){
		  calbtn = document.getElementById("line-btn2");
		  calbtn.innerText = "较低";
		  calbtn = document.getElementById("line-btn3");
		  calbtn.innerText = "较低";
	  }
	  if (lv == 3){
		  calbtn = document.getElementById("line-btn1");
		  calbtn.setAttribute("class", "btn-tot btn-lv3");
		  calbtn.innerText = "lv3";
	  }else if (lv == 4){
		  calbtn = document.getElementById("line-btn1");
		  calbtn.setAttribute("class", "btn-tot btn-lv4");
		  calbtn.innerText = "lv4";
	  } else if (lv == 5){
		  calbtn = document.getElementById("line-btn1");
		  calbtn.setAttribute("class", "btn-tot btn-lv5");
		  calbtn.innerText = "lv5";
	  } else if (lv == 6){
		  calbtn = document.getElementById("line-btn1");
		  calbtn.setAttribute("class", "btn-tot btn-lv6");
		  calbtn.innerText = "lv6";
	  }
	  
	  if (!sex){
		  calbtn = document.getElementById("line-btn4");
		  calbtn.setAttribute("class", "btn-tot btn-girl");
		  calbtn.innerText = "女性";
		  calbtn = document.getElementById("line-btn5");
		  calbtn.setAttribute("class", "btn-tot btn-fs");
		  calbtn.innerText = "娱乐";
		  calbtn = document.getElementById("line-btn6");
		  calbtn.setAttribute("class", "btn-tot btn-fs");
		  calbtn.innerText = "舞蹈";
		  calbtn = document.getElementById("line-btn7");
		  calbtn.setAttribute("class", "btn-tot btn-fs");
		  calbtn.innerText = "生活";
	  }
	  
	  if (!fensan){
		  calbtn = document.getElementById("line-btn8");
		  calbtn.setAttribute("class", "btn-tot btn-jz");
		  calbtn.innerText = "较为集中";
		  calbtn = document.getElementById("line-btn9");
		  calbtn.setAttribute("class", "btn-tot");
		  calbtn.innerText = "较短";
		  calbtn = document.getElementById("line-btn10");
		  calbtn.setAttribute("class", "btn-tot");
		  calbtn.innerText = "较低";
	  }
	  
  }

$(function () {
  sexnum();
  line2();
  level();
  detail();
  comment();
  
  
  
  setTimeout("cal()",1000);
  
  
  function sexnum() {
    var chartDom = document.getElementById('sexnum');
    var myChart = echarts.init(chartDom);
  
    $.ajax({
      contentType: "application/json;charset=utf-8",
      type: "get",
      url: "http://localhost:8181/hotComment/getCommentSex?bv=" + bv,
      // data: JSON.stringify(myUser),
      dataType: "json",
      success: function(res) {
        // console.log(res);
        if (res) {
			if (res[0].male < res[0].female){
				sex = false;
			}
			// console.log(sex);
				
          myChart.setOption({
            color: ['rgba(255,215,0,0.7)', "rgba(188,238,104,0.7)", "rgba(100,149,237,0.7)"],
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'shadow'
              }
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              top: 'top',
              containLabel: true
            },
            xAxis: {
              type: 'value',
              boundaryGap: [0, 0.01],
              splitLine: {
                lineStyle: {
                  color: '#4A708B',
                }
              }
            },
            yAxis: {
              type: 'category',
              data: ['男', '女', '保密']
            },
            series: [
              {
                name: '人数',
                type: 'bar',
                data: [res[0].male, res[0].female, res[0].secret],
                colorBy: 'data',
                legendHoverLink: false,
              },
            ]
          })
        }
      },
      error: function(errorMag) {
        alert("用户状态分析数据加载失败");
      }
    })
  }
  
  function line2() {
    var chartDom = document.getElementById('line2');
    var myChart = echarts.init(chartDom);
    $.ajax({
    contentType: "application/json;charset=utf-8",
    type: "get",
    url: "http://localhost:8181/hotBarrage/getSendTimes?bvid=" + bv,
    // data: JSON.stringify(myUser),
    dataType: "json",
    success: function(res) {
      // console.log(res[0].two);
	  // console.log("=============");
	  // console.log(res);
      var days = [];
      var count = [];
	  var last = res[0].count;
	  var ave = 0;
	  var tot = 0;
      for (let index = 0; index < res.length; index++) {
        days.push(res[index].days);
		count.push(res[index].count);
		ave += Math.abs(res[index].count - last);
		tot += res[index].count;
		last = res[index].count;
      }
	  // console.log(tot / days.length);
	  // console.log(ave / 5);
	  if (tot / days.length < ave / 5 ){
		  fensan = false;
	  }
      if (res) {
        myChart.setOption({
          tooltip: {
            trigger: "axis",
          },
          xAxis: {
            type: 'category',
            data: days,
          },
          yAxis: [{
            type: 'value',
            splitNumber:3,
            min: 0,
            splitLine: {
              lineStyle: {
                color: '#4A708B',
              }
            },
          }],
          grid: {
            top: '15',
            rught: 'center'
          },
          series: [
            {
              data: count,
              type: 'line',
              smooth: true
            }
          ]
        })
      }
    },
    error: function(errorMag) {
      alert("用户状态分析数据加载失败");
    }
  })
  }

  function level() {
    var chartDom = document.getElementById('level');
    var myChart = echarts.init(chartDom);

    $.ajax({
      contentType: "application/json;charset=utf-8",
      type: "get",
      url: "http://localhost:8181/hotComment/getCommentSex?bv=" + bv,
      // data: JSON.stringify(myUser),
      dataType: "json",
      success: function(res) {
		  var levels = [];
		  levels.push(res[0].zero);
		  levels.push(res[0].one);
		  levels.push(res[0].two);
		  levels.push(res[0].three);
		  levels.push(res[0].four);
		  levels.push(res[0].five);
		  levels.push(res[0].six);
		  
		  var max_num = 0;
		  for (var i = 0; i <= 6; i ++){
			  if (levels[i] > max_num){
				  max_num = levels[i];
				  lv = i;
			  }
		  }
        // console.log(res[0].two);
        var getData = [];
        getData.push(
                    {value: res[0].zero+1, name: 'lv0'},
                    {value: res[0].two,name: 'lv2'}, 
                    {value: res[0].three, name: 'lv3'},
                    {value: res[0].four, name: 'lv4'},
                    {value: res[0].five, name: 'lv5'},
                    {value: res[0].six, name: 'lv6'},
                    )
        // console.log(getData);
        if (res) {
          myChart.setOption({
            color: ["#0082fc","#09b0d3","#1d27c9","#009db2","#024b51","#0780cf","#765005"],
            tooltip: {
              trigger: 'item'
            },
            legend: {
              top: '0',
              textStyle: {
                color: "#fff"
              },
            },
            grid: {
              top: 'top',
              containLabel: true
            },
            series: [
              {
                name: 'level',
                type: 'pie',
                radius: ['25%', '70%'],
                top: 0,
                avoidLabelOverlap: false,
                emphasis: {
                  label: {
                    show: true,
                    fontSize: '40',
                    fontWeight: 'bold'
                  }
                },
                labelLine: {
                  show: false
                },
                label: {
                  show: false,
                },
                data: getData
              }
            ]
          })
        }
      },
      error: function(errorMag) {
        alert("用户状态分析数据加载失败");
      }
    })
  }

  function detail() {
    $.ajax({
      contentType: "application/json;charset=utf-8",
      type: "get",
      url: "http://localhost:8181/hotComment/getDetail?bv=" + bv,
      // data: JSON.stringify(myUser),
      dataType: "json",
      success: function(res) {
        // console.log(res[0].pic);
        if (res) {
          // $('view').innerHtml(res[0].views)
          $('.view').html(res[0].views)
          $('.likes').html(res[0].likes)
          $('.coin').html(res[0].coin)
          $('.favorite').html(res[0].favorite)
          $('.reply').html(res[0].reply)
          $('.share').html(res[0].share)
          $('.danmu').html(res[0].danmaku)
          $('#picTil').html(res[0].title)
          $('#pic').attr("src", res[0].pic)
        }
      },
      error: function(errorMag) {
        alert("用户状态分析数据加载失败");
      }
    })
  }

  function comment() {
	  var btn = document.getElementById("btn-r-b");
	  var value = 1;
	  
    $.ajax({
      contentType: "application/json;charset=utf-8",
      type: "get",
      url: "http://localhost:8181/hotComment/getHotComment?bv=" + bv,
      // data: JSON.stringify(myUser),
      dataType: "json",
      success: function(res) {
        // console.log(res[0].views);
        if (res) {
          // $('view').innerHtml(res[0].views)
          for (let index = 0; index < 200; index++) {
            
            $(".commentD").append("<li>" + "<div style=\"overflow: hidden;padding-bottom: 3px\"><div style=\"float: left; \">" + res[index].username +  "</div>" + "<div style=\"float: right\">" + "❤" + res[index].likes + "</div></div>" + res[index].content + "</li><br/>")
          }
        }
		
      },
      error: function(errorMag) {
        alert("用户状态分析数据加载失败");
      }
    })
	
	btn.onclick = function(){
		console.log(value);
		if (value == 1){
			value = 2;
			var e = document.getElementById("comment");
			var ull = e.getElementsByTagName("ul")[0];
			var child = ull.lastElementChild;
			var tt = e.parentNode.getElementsByTagName("span")[0];
			tt.innerText = "弹幕"
			
			while(child){
				// console.log(child);
				child.parentNode.removeChild(child);
				// e.removeChild(child);
				child = ull.lastElementChild;
			}
			$.ajax({
			  contentType: "application/json;charset=utf-8",
			  type: "get",
			  url: "http://localhost:8181/hotBarrage/getContents?bvid=" + bv,
			  // data: JSON.stringify(myUser),
			  dataType: "json",
			  success: function(res) {
			    // console.log(res[0].views);
			    if (res) {
			      // $('view').innerHtml(res[0].views)
			      for (let index = 0; index < 100; index++) {
			        
			        $(".commentD").append("<li>" + "<div style=\"overflow: hidden;\">" + res[index] + "</div>"  + "</li><br/>")
			      }
			    }
			  },
			  error: function(errorMag) {
			    alert("用户状态分析数据加载失败");
			  }
			})
			
		}else{
			value = 1;
			var e = document.getElementById("comment");
			var ull = e.getElementsByTagName("ul")[0];
			var child = ull.lastElementChild;
			var tt = e.parentNode.getElementsByTagName("span")[0];
			tt.innerText = "评论"
			
			while(child){
				// console.log(child);
				child.parentNode.removeChild(child);
				// e.removeChild(child);
				child = ull.lastElementChild;
			}
			$.ajax({
			  contentType: "application/json;charset=utf-8",
			  type: "get",
			  url: "http://localhost:8181/hotComment/getHotComment?bv=" + bv,
			  // data: JSON.stringify(myUser),
			  dataType: "json",
			  success: function(res) {
			    // console.log(res[0].views);
			    if (res) {
			      // $('view').innerHtml(res[0].views)
			      for (let index = 0; index < 200; index++) {
			        
			        $(".commentD").append("<li>" + "<div style=\"overflow: hidden;padding-bottom: 3px\"><div style=\"float: left; \">" + res[index].username +  "</div>" + "<div style=\"float: right\">" + "❤" + res[index].likes + "</div></div>" + res[index].content + "</li><br/>")
			      }
			    }
			  },
			  error: function(errorMag) {
			    alert("用户状态分析数据加载失败");
			  }
			})
		}
	}
  }
})
